package com.yiren.dbaa.global;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import androidx.multidex.MultiDex;

import com.baidu.mapapi.SDKInitializer;
import com.tencent.bugly.crashreport.CrashReport;
import com.yiren.backstage.App;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.StationParamConfigBean;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.dbaa.module.common.view.activity.MainActivity;
import com.yiren.dbaa.module.param_config.api.ParamConfigApi;
import com.yiren.dbaa.util.ActivityManager;

/**
 * 类名称：GlobalApplication
 * 创建人：lwb
 * 创建时间：2021/11/12
 */
public class GlobalApplication extends App {

    private static GlobalApplication sInstance;

    static {
        System.loadLibrary("native_lib");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
//        CrashReport.initCrashReport(getApplicationContext(), "5df1221fa7", true);

        SDKInitializer.initialize(this);

        //SpeechUtility.createUtility(getApplicationContext(), "appid=5a693069," + SpeechConstant.FORCE_LOGIN + "=true");
//        PreferencesUtil.savePreferences(getApplicationContext(), ConfigCodeEnum.ACCESS_TOKEN.toString(), G.access_token,false);
        CrashReport.initCrashReport(getApplicationContext(), "5c690bdced", false);
//        Bugly.init(this, "5c690bdced", BuildConfig.DEBUG);
    }

    public void logOut(Activity activity) {
        ActivityManager.getActivityManager().exit();
//        Studentdao studentDao = getSession().getStudentDao();
//        studentDao.deleteAll();
        StationParamConfigBean stationParamConfig = LoadConfigApi.loadStationParamConfig(activity);
        stationParamConfig.setBaseStationId("");
        stationParamConfig.setBaseStationName("");
        stationParamConfig.setRoverIccid("");
        stationParamConfig.setRoverRegdt("");
        stationParamConfig.setRoverVersion("");
        ParamConfigApi.saveStationParamConfig(activity, stationParamConfig);

        UserParamConfigBean userParamConfig = LoadConfigApi.loadUserParamConfig(activity);
        userParamConfig.setAccessToken("");
        userParamConfig.setRefreshToken("");
        userParamConfig.setUserInfo(null);
        ParamConfigApi.saveUserParamConfig(activity, userParamConfig);

        Intent intent = new Intent(activity, MainActivity.class);
        intent.putExtra("isLogOut", true);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    public static GlobalApplication getInstance() {
        return sInstance;
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }

}
